import axios from 'axios'
export function getBaseInfo () {
  const params = {}
  return axios.get('user/info', params).then(result => {
    if (result.isSuccess) {
      const data = result.data
      const roles = []
      result.data.roles.forEach(element => {
        roles.push(element.name)
      })
      result.data = {
        id: data.id,
        name: data.name,
        userName: data.username,
        nickName: data.nickname,
        lastLoginTime: data.lastLoginTime,
        lastLoginIp: data.lastLoginIp,
        avatar: data.photo,
        roles: roles
      }
    }
    return result
  })
}

export function logout () {
  const params = {}
  return axios.post('logout', params)
}

/**
 * 获取指定页面的权限值
 * @param {String} pageName 页面的 name
 */
export function getUserPagePermission (pageName) {
  const params = {
  }
  return axios.get(`../api/upper/permission/page/${pageName}`, { params }).then(result => {
    if (result.isSuccess) {
      // result.data = result.data
    } else {
      result.data = 0
    }
    return result
  })
}

/**
 *  获取指定用户的所有页面权限
 * @param {String} userId 用户ID
 */
export function getUserAllPagePermission (userId) {
  const params = {
    userId: userId
  }
  return axios.get('../../api/upper/user/page-permissions', { params }).then(result => {
    const map = {}
    if (result.isSuccess) {
      result.data.forEach(page => {
        map[page.pageName] = page.permission
      })
    }
    return map
  })
}

/**
 * 获取用户的权限总和
 * @param {String} userId 用户ID
 */
export function getUserPermissionSum (userId) {
  const params = {
    userId: userId
  }
  return axios.get('../../api/upper/user/permissions', { params })
}
